package com.younion.action.consumer.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IActiveService;
import com.younion.common.constants.SysConstants;
import com.younion.model.ApiAllActiveResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.util.config.ConfigHelper;
import com.younion.vo.ApiAllActiveVO;

public class AllActivitysGetConsumer extends AbstractRequestConsumer {
	private IActiveService activeService;

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return getActivity(request, response);
	}

	public String getActivity(HttpServletRequest request,
			HttpServletResponse response) {
		List<ApiAllActiveVO> apiAllActiveVOList = null;
		List<ApiAllActiveResult> activitysResultList = new ArrayList<ApiAllActiveResult>();
		ResultObject resultObject = new ResultObject();
		try {
			String img_url_pre = ConfigHelper.fastGetConfig("interface",
					"img_url_pre", "http://localhost:8080/younonmember/");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			//搜索
			String searchType = request.getParameter("search_type");
			String searchKey = request.getParameter("search_key");
			if(HttpRequestUtils.checkIsEmptyParam("search_type", searchType, response)){
				if(!HttpRequestUtils.checkNeedIntegerParam("search_type", searchType, response))
					return null;
			}
			Map paramMap = new HashMap();
			paramMap.put("searchType", searchType);
			paramMap.put("searchKey", searchKey);
			apiAllActiveVOList = (List<ApiAllActiveVO>) activeService
					.getApiAll(paramMap);
			if (apiAllActiveVOList != null && apiAllActiveVOList.size() > 0) {
				for (ApiAllActiveVO apiAllActiveVO : apiAllActiveVOList) {
					ApiAllActiveResult apiAllActiveResult = new ApiAllActiveResult();
					apiAllActiveResult.setId(apiAllActiveVO.getId().toString());
					apiAllActiveResult.setTitle(apiAllActiveVO.getTitle());
					apiAllActiveResult.setEndTime(sdf.format(apiAllActiveVO
							.getEndTime()));
					apiAllActiveResult.setStartTime(sdf.format(apiAllActiveVO
							.getStartTime()));
					apiAllActiveResult.setIs_new("1");// TODO 算出最新
					apiAllActiveResult.setIs_recommend(apiAllActiveVO
							.getIs_recommend());
					apiAllActiveResult.setShop(apiAllActiveVO.getCustomer()
							.getShops());
					apiAllActiveResult
							.setCategory_id(apiAllActiveVO.getCustomer()
									.getCustomerKindId() != null ? apiAllActiveVO
									.getCustomer().getCustomerKindId()
									.toString()
									: "");
					apiAllActiveResult.setPic(img_url_pre
							+ SysConstants.ACTIVE_IMG_URL + "/"
							+ apiAllActiveVO.getId() + "/"
							+ apiAllActiveVO.getPicName());
					apiAllActiveResult.setIs_hot(apiAllActiveVO.getIs_hot());
					apiAllActiveResult.setIs_recommend(apiAllActiveVO
							.getIs_recommend());
					apiAllActiveResult.setInstruction(apiAllActiveVO
							.getInstruction());
					apiAllActiveResult.setDescription(apiAllActiveVO
							.getDescription());
					activitysResultList.add(apiAllActiveResult);
				}
			}
			resultObject.setResult(activitysResultList);
			resultObject.setStatus(ApiConstant.SUCCESS_STATUS);

		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	public IActiveService getActiveService() {
		return activeService;
	}

	public void setActiveService(IActiveService activeService) {
		this.activeService = activeService;
	}

}
